package com.hmsproject.socialAuth;

import java.io.InputStream;
import java.io.Serializable;
import java.net.URL;
import java.util.Map;
import java.util.Properties;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.brickred.socialauth.AuthProvider;
import org.brickred.socialauth.Profile;
import org.brickred.socialauth.SocialAuthConfig;
import org.brickred.socialauth.SocialAuthManager;
import org.brickred.socialauth.util.SocialAuthUtil;

import com.hmsproject.db.util.HMSDBUtil;
import com.hmsproject.search.managedbean.SearchView;

@ManagedBean
@SessionScoped
public class FacebookBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private SocialAuthManager manager;

	private boolean dealPostedOnFb;

	String FACEBOOK_APP_ID = "401532843215071";

	String FACEBOOK_APP_SECRET = "1773c2fe4a434b4723077f104b226117";

	@ManagedProperty("#{searchView}")
	private SearchView searchView;


	private Profile profile = new Profile();

	private AuthProvider provider;

	public SocialAuthManager getManager() {
		return manager;
	}

	public void setManager(SocialAuthManager manager) {
		this.manager = manager;
	}

	public Profile getProfile() {
		return profile;
	}

	public void setProfile(Profile profile) {
		this.profile = profile;
	}

	public SearchView getSearchView() {
		return searchView;
	}

	public void setSearchView(SearchView searchView) {
		this.searchView = searchView;
	}

	public boolean isDealPostedOnFb() {
		return dealPostedOnFb;
	}

	public void setDealPostedOnFb(boolean dealPostedOnFb) {
		this.dealPostedOnFb = dealPostedOnFb;
	}


	private void postMessageOnFb() {

		if (provider != null) {

			try {
	
				InputStream inputStream = new URL("http://localhost:8080"+"/hms/resources/image/h1.jpg").openStream();
				provider.uploadImage(" Got a awesome Deal in Hotelee  "
						+ searchView.getDealView().getDealData().getDealUrl(), "hotel.jpg", inputStream);
		
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				dealPostedOnFb = false;
			}

		}

	}

	public void prepareMessageForFbPosting() throws Exception {
		dealPostedOnFb = true;
		if (manager == null) {
			this.socialConnect();
		}
		this.postMessageOnFb();
	}

	public String pullUserInfo() {

		try {
			// Pull user's data from the provider

			ExternalContext externalContext = FacesContext.getCurrentInstance()
					.getExternalContext();
			HttpServletRequest request = (HttpServletRequest) externalContext
					.getRequest();

			if (manager != null) {

				Map<String, String> paramsMap = SocialAuthUtil
						.getRequestParametersMap(request);
				provider = manager.connect(paramsMap);
				profile = provider.getUserProfile();

				// Do what you want with the data (e.g. persist to the database,
				// etc.)
				System.out.println("User's Social profile: " + profile
						+ " LIST " + provider.getContactList().toString());
			}
		} catch (Exception ex) {
			System.out.println("Excetpion " + ex.toString());
		}
		return profile.toString();

	}

	public void socialConnect() throws Exception {
		// Put your keys and secrets from the providers here
		Properties props = System.getProperties();
		props.put("graph.facebook.com.consumer_key", FACEBOOK_APP_ID);
		props.put("graph.facebook.com.consumer_secret", FACEBOOK_APP_SECRET);
		props.put("graph.facebook.com.custom_permissions",
				"publish_stream,email,user_birthday,user_location,offline_access");

		SocialAuthConfig config = SocialAuthConfig.getDefault();

		config.load(props);
		manager = new SocialAuthManager();
		manager.setSocialAuthConfig(config);
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) context.getExternalContext()
				.getSession(true);
		String successURL = "http://localhost:8080"+"/hms/" + ";/";
		String authenticationURL = manager.getAuthenticationUrl("facebook",
				successURL);

		session.setAttribute("authManager", manager);

		FacesContext.getCurrentInstance().getExternalContext()
				.redirect(authenticationURL);

	}
}
